import { defineComponent, useSlots, ref, watch, openBlock, createElementBlock, unref, renderSlot, createCommentVNode } from "vue";
const _hoisted_1 = { class: "me-chat-panel" };
const _hoisted_2 = {
  key: 0,
  class: "me-chat-panel-header"
};
const _hoisted_3 = {
  key: 2,
  class: "me-chat-panel-footer"
};
const _sfc_main = /* @__PURE__ */ defineComponent({
  ...{
    name: "me-chat-panel"
  },
  __name: "chat-panel",
  props: {
    autoScrollToBottom: { type: Boolean, default: true }
  },
  setup(__props, { expose: __expose }) {
    const props = __props;
    const slots = useSlots();
    const autoScroll = ref(props.autoScrollToBottom);
    watch(() => props.autoScrollToBottom, () => {
      autoScroll.value = props.autoScrollToBottom;
    });
    const bodyRef = ref();
    const useMouseWheel = ref(false);
    const scrollToBottom = () => {
      if (bodyRef.value && autoScroll.value) {
        bodyRef.value.scrollTo({
          top: bodyRef.value.scrollHeight
        });
        useMouseWheel.value = false;
      }
    };
    const scrollTo = (offset) => {
      if (bodyRef.value) {
        bodyRef.value.scrollTo({
          top: offset
        });
        useMouseWheel.value = false;
      }
    };
    const scrollToTop = () => {
      if (bodyRef.value) {
        bodyRef.value.scrollTo({
          top: 0
        });
        useMouseWheel.value = false;
      }
    };
    __expose({
      scrollToBottom,
      scrollTo,
      scrollToTop
    });
    const onScroll = () => {
      if (useMouseWheel.value) {
        const scrollTop = bodyRef.value.scrollTop;
        const height = bodyRef.value.clientHeight;
        const scrollHeight = bodyRef.value.scrollHeight;
        if (scrollTop + height + 10 >= scrollHeight) {
          autoScroll.value = true;
        } else {
          autoScroll.value = false;
        }
      }
    };
    const onMouseWheel = () => {
      useMouseWheel.value = true;
    };
    return (_ctx, _cache) => {
      return openBlock(), createElementBlock("div", _hoisted_1, [
        unref(slots).header ? (openBlock(), createElementBlock("div", _hoisted_2, [
          renderSlot(_ctx.$slots, "header")
        ])) : createCommentVNode("", true),
        unref(slots).body ? (openBlock(), createElementBlock("div", {
          key: 1,
          ref_key: "bodyRef",
          ref: bodyRef,
          class: "me-chat-panel-body",
          onScroll,
          onWheel: onMouseWheel
        }, [
          renderSlot(_ctx.$slots, "body")
        ], 544)) : createCommentVNode("", true),
        unref(slots).footer ? (openBlock(), createElementBlock("div", _hoisted_3, [
          renderSlot(_ctx.$slots, "footer")
        ])) : createCommentVNode("", true)
      ]);
    };
  }
});
export {
  _sfc_main as default
};
//# sourceMappingURL=chat-panel.vue2.mjs.map
